package cn.sytton.taffe.web.sys.service.impl;

import cn.sytton.taffe.framework.base.service.BaseService;
import cn.sytton.taffe.web.sys.entity.Auth;
import cn.sytton.taffe.web.sys.entity.DeptMenuEntity;
import cn.sytton.taffe.web.sys.entity.DeptMenuFuncEntity;
import cn.sytton.taffe.web.sys.mapper.DeptMenuFuncMapper;
import cn.sytton.taffe.web.sys.mapper.DeptMenuMapper;
import cn.sytton.taffe.web.sys.service.IDeptMenuService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 部门菜单服务层
 *
 * @author skyrock
 */
@Service
@RequiredArgsConstructor
@Transactional(rollbackFor = Exception.class)
public class DeptMenuService extends BaseService<DeptMenuMapper, DeptMenuEntity> implements IDeptMenuService {

    private final DeptMenuFuncMapper deptMenuFuncMapper;

    @Override
    public Map<String, Object> findMenu(String deptId) {
        Map<String, Object> result = new HashMap<>();
        if (StringUtils.hasText(deptId)) {
            List<String> menuIds = list(new LambdaQueryWrapper<DeptMenuEntity>()
                    .eq(DeptMenuEntity::getDeptId, deptId))
                    .stream().map(DeptMenuEntity::getMenuId)
                    .collect(Collectors.toList());
            List<String> funcKeys = deptMenuFuncMapper.selectList(new LambdaQueryWrapper<DeptMenuFuncEntity>()
                            .eq(DeptMenuFuncEntity::getDeptId, deptId))
                    .stream().map(f->f.getMenuId()+"#"+f.getFuncKey())
                    .collect(Collectors.toList());

            result.put("menuIds", menuIds);
            result.put("funcKeys", funcKeys);
        } else {
            result.put("menuIds", new ArrayList<>());
            result.put("funcKeys", new ArrayList<>());
        }
        return result;
    }

    @Override
    public void authMenu(Auth auth) {
        for(String menuIdFuncKey : auth.getDeleteMenuIdFuncKeys()) {
            if (StringUtils.hasText(menuIdFuncKey)) {
                String[] falseFuncKeyObject = menuIdFuncKey.split("#");
                deptMenuFuncMapper.delete(new LambdaQueryWrapper<DeptMenuFuncEntity>()
                        .eq(DeptMenuFuncEntity::getDeptId, auth.getDeptId())
                        .eq(DeptMenuFuncEntity::getMenuId, falseFuncKeyObject[0])
                        .eq(DeptMenuFuncEntity::getFuncKey, falseFuncKeyObject[1]));
            }
        }
        for (String menuId : auth.getDeleteMenuIds()) {
            if (StringUtils.hasText(menuId)) {
                remove(new LambdaQueryWrapper<DeptMenuEntity>()
                        .eq(DeptMenuEntity::getDeptId, auth.getDeptId())
                        .eq(DeptMenuEntity::getMenuId, menuId));
            }
        }
        for (String menuId : auth.getAddMenuIds()) {
            if (StringUtils.hasText(menuId)) {
                DeptMenuEntity deptMenuEntity = new DeptMenuEntity();
                deptMenuEntity.setMenuId(menuId);
                deptMenuEntity.setDeptId(auth.getDeptId());
                baseMapper.create(deptMenuEntity);
            }
        }
        for (String menuIdFuncKey : auth.getAddMenuIdFuncKeys()) {
            if (StringUtils.hasText(menuIdFuncKey)) {
                String[] trueFuncKeyObject = menuIdFuncKey.split("#");
                String menuId = trueFuncKeyObject[0];
                String funcKey = trueFuncKeyObject[1];
                DeptMenuFuncEntity deptMenuFuncEntity = new DeptMenuFuncEntity();
                deptMenuFuncEntity.setMenuId(menuId);
                deptMenuFuncEntity.setFuncKey(funcKey);
                deptMenuFuncEntity.setDeptId(auth.getDeptId());
                deptMenuFuncMapper.create(deptMenuFuncEntity);
            }
        }
    }
}
